package com.coldchain.admin.config.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.coldchain.admin.config.entity.GoodsClassify;
import com.coldchain.admin.config.mapper.GoodsClassifyMapper;
import com.coldchain.admin.config.service.IGoodsClassifyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coldchain.common.util.Pinyin4j;
import com.coldchain.common.util.TreeUtil;
import com.fangliai.core.context.BaseContextHandler;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fangliai
 * @since 2018-11-09
 */
@Service
public class GoodsClassifyServiceImpl extends ServiceImpl<GoodsClassifyMapper, GoodsClassify> implements IGoodsClassifyService {

    @Override
    public boolean edit(GoodsClassify goodsClassify) throws Exception {
//        ValidatorUtils.validateEntity(goodsClassify,GoodsClassify.class);
        if(goodsClassify.getGoodsClassifyId()==null){
            goodsClassify.setCreateBy(BaseContextHandler.getUsername());
        }
        goodsClassify.setPinyin(new Pinyin4j().toPinYinUppercase(goodsClassify.getName()));
        return super.saveOrUpdate(goodsClassify);
    }

    @Override
    public List<GoodsClassify> getGoodsClassTree() {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.orderByDesc("sort","createTime","pinyin");
        List<GoodsClassify> goodsClassifies=list(queryWrapper);
        return new TreeUtil<GoodsClassify>().buildByRecursive(goodsClassifies,0);
    }
}
